﻿using System;
using System.Data;
using System.Text;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Da.Db.POS;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class SoDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var so = Request["so"];
            if (!CharUtil.IsValidateHash(so))
            {
                return;
            }
            var dba = new RDBEngine();

            dba.AddTable(DSoHeader.TABLE);
            dba.AddWhere(DSoHeader.ID, so);
            var table = dba.ExecuteSelect();

            if (table.Rows.Count < 1)
            {
                return;
            }

            DataRow row = table.Rows[0];
            DateTime dt = (DateTime)row[DSoHeader.UPDATE_TIME];
            LlCode.Text = row[DSoHeader.CODE] as string;//销售单号
            LlDate.Text = dt.ToShortDateString().ToString();//销售日期
            LlEmp.Text = row[DSoHeader.TERMINAL] as string;//收银员
            var id = row[DSoHeader.VIP_ID] as string;//会员id

            dba.Reset();
            dba.AddTable(DVip.TABLE);
            dba.AddWhere(DVip.ID, id);//SoHeader中读取的VIP_ID

            table = dba.ExecuteSelect();
            if (table.Rows.Count > 0)
            {
                row = table.Rows[0];
                LlUserName.Text = row[DVip.DISPLAY_NAME] as string;//会员名称
                LlLevel.Text = row[DVip.LEVEL_NAME] as string;//会员等级
            }

            dba.Reset();
            ListSoDetail(dba,so);
        }

        private void ListSoDetail(RDBEngine dba,string so)
        {
            var sql = new StringBuilder();
            sql.Append("SELECT ");
            sql.Append(" a.").Append(DSpu.CODE);
            sql.Append(",a.").Append(DSpu.NAME);
            sql.Append(",a.").Append(DSpu.SPEC);
            sql.Append(",a.").Append(DSpu.MODE);
            sql.Append(",b.").Append(DSoDetail.SPU_PRICE);
            sql.Append(",b.").Append(DSoDetail.VIP_PRICE);
            sql.Append(",b.").Append(DSoDetail.PRO_PRICE);
            sql.Append(",b.").Append(DSoDetail.SPU_QTY);
            sql.Append(",b.").Append(DSoDetail.SUBTOTAL);
            sql.Append(" FROM ").Append(DSoDetail.TABLE).Append(" b, ").Append(DSpu.TABLE).Append(" a");
            sql.Append(" WHERE b.").Append(DSoDetail.SPU_ID).Append("=a.").Append(DSpu.ID);
            sql.Append(" AND b.").Append(DSoDetail.SO_ID).Append("='").Append(so).Append("'");

            RpSpus.DataSource = dba.ExecuteSelect(sql.ToString());
            RpSpus.DataBind();
        }
    }
}

